<!--- CacheManager.cfc --->
<!---
	* Date:26/1/2010 09:49:26 
	* Author: Cristian Costantini 
	* Version: 1.0.0 beta 2
	* License: http://www.apache.org/licenses/LICENSE-2.0
	 --->
	
<!--- 

<bean id="modelGlue.cacheAdapter" class="cacheManagement.adapter.modelglue.CacheAdapter">
	<constructor-arg name="defaultTypeToCache"><value>ModelGlue.gesture.view.Content</value></constructor-arg>
	<constructor-arg name="defaultTimeout"><value>60</value></constructor-arg>
	<constructor-arg name="config"><value>config/ehcache.xml</value></constructor-arg>
</bean>

 --->
	
	
<cfcomponent output="false" hint="I'm a cache manager adapter for ModelGlue.">

	<cffunction name="init" access="public" returntype="Component" output="false">
		<cfargument name="config" type="String" required="true" />
		<cfargument name="defaultTimeOut" type="Numeric" required="true" />
		<cfargument name="defaultTypeToCache" type="String" required="true" />
		<cfset var path = expandPath( config ) />
		
		<cfset variables.defaultTimeOut =arguments.defaultTimeOut />
		<cfset variables.defaultTypeToCache = arguments.defaultTypeToCache />
		
		<cfif NOT fileExists( path )>
		
			<cfset path = expandPath( '/cacheManagement/adapter/modelglue/ehcache.xml' ) />
		
		</cfif>
		
		<cfset variables.cacheManager = createObject( 'component','cacheManagement.CacheManager' ).init( path ) />
		<cfreturn this />
	</cffunction>
	
	<cffunction name="put" access="public" hint="Puts content into the cache.">
		<cfargument name="key" type="String" required="true" hint="Key for the content." />
		<cfargument name="content" type="String" required="true" hint="The content to cache." />
		<cfargument name="timeout" type="Numeric" required="false" default="#variables.defaultTimeOut#" hint="Seconds this item should live in the cache." />
		<cfargument name="class" type="String" required="false" default="#variables.defaultTypeToCache#" />
		<cfscript>
		var cache = "";
		var element = variables.cacheManager.createElement( arguments.key, arguments.content, false, 1, arguments.timeOut );
		
		if( variables.cacheManager.cacheExists( arguments.class ) ){
		
			variables.cacheManager.put( arguments.class, element );
		
		}else{
			
			throw( "Missing cache !", 'cacheManagement.CacheManagerAdapter' );
		
		}
		</cfscript>
	</cffunction>
	
	<cffunction name="get" access="public" returntype="struct" hint="Gets content from the cache.  Returns struct of {success:true, content=""content""}.  If not found, success will be false.">
		<cfargument name="key" type="string" hint="Key for the content." />
		<cfargument name="class" type="String" required="false" default="#variables.defaultTypeToCache#" />
		<cfset var result = structNew() />
		<cftry>
			
			<cfset result.content = variables.cacheManager.get( arguments.cache, arguments.key ) />
			<cfset result.success = true />
				
			<cfcatch type="any">
				<cfset result.success = true />
			</cfcatch>
			
		</cftry>
		<cfreturn result />
	</cffunction>
	
	<cffunction name="purge" access="public" returntype="struct" hint="Purges content from the cache.">
		<cfargument name="key" type="string" hint="Key for the content." />
		<cfargument name="class" type="String" required="false" default="#variables.defaultTypeToCache#" />
		<cfset variables.cacheManager.removeElement( arguments.cache, arguments.key ) />
	</cffunction>
	
	<!---  --->
	
	<cffunction name="clearStatistics" access="public" returntype="void" >
		<cfargument name="class" type="String" required="false" default="#variables.defaultTypeToCache#" />
		<cfset variables.cacheManager.clearStatistics( arguments.class ) />
	</cffunction>
	
	<cffunction name="getStatistics" access="public" returntype="Any" >
		<cfargument name="class" type="String" required="false" default="#variables.defaultTypeToCache#" />
		<cfreturn variables.cacheManager.getStatistics( arguments.class ) />
	</cffunction>
	
	<!---  --->
	
	<cffunction name="sweep" access="public" returntype="void" hint="Instructs implementation to sweep stale items.">
		<cfabort showerror="Method not implemented !" />
	</cffunction>
	
	<cffunction name="getContents" access="public" returntype="struct" hint="Gets information about cache contents.">
		<cfabort showerror="Method not implemented !" />
	</cffunction>
	
	<cffunction name="throw" access="private" returntype="Any" output="false">
		<cfargument name="message" type="String" required="true" />
		<cfargument name="type" type="String" required="false" default="Any" />
		<cfargument name="detail" type="String" required="false" default="" />
		<cfthrow message="#message#" type="#type#" detail="#detail#" />
	</cffunction>

</cfcomponent>